package com.business.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.business.system.dao.TabRepairFlowMapper;
import com.business.system.dao.TabRepairGjpgMapper;
import com.business.system.pojo.TabRepairFlow;
import com.business.system.pojo.TabRepairGjpg;
import com.business.system.pojo.TabRepairGjpgExample;
import com.business.system.service.ITabRapairGJPGService;
import com.utility.Page;

@Service
public class TabRepairGJPGServiceImpl implements ITabRapairGJPGService {

	@Autowired
	private TabRepairGjpgMapper repairGjpgMapper;
	@Autowired
	private TabRepairFlowMapper repairFlowMapper;

	@Override
	public int delete(String id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.repairGjpgMapper.deleteByPrimaryKey(id);
	}

	@Override
	public TabRepairGjpg findById(String id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.repairGjpgMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TabRepairGjpg> findList(TabRepairGjpg entity)
			throws RuntimeException {
		// TODO Auto-generated method stub
		TabRepairGjpgExample example = new TabRepairGjpgExample();
		if (entity.getFlowId() != null && !"".equals(entity.getFlowId())) {
			example.createCriteria().andFlowIdEqualTo(entity.getFlowId());
		}
		return this.repairGjpgMapper.selectByExample(example);
	}

	@Override
	public Page<TabRepairGjpg> findListPage(TabRepairGjpg entity, int pageNo,
			int pageSize) throws RuntimeException {
		// TODO Auto-generated method stub
		Page<TabRepairGjpg> page = new Page<TabRepairGjpg>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flowId", entity.getFlowId());
		page.setParams(map);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		List<TabRepairGjpg> list = this.repairGjpgMapper.findListPage(page);
		page.setResults(list);
		return page;
	}

	@Override
	public int insert(TabRepairGjpg entity) throws RuntimeException {
		// TODO Auto-generated method stub
		if (entity.getFlowId() != null && !"".equals(entity.getFlowId())) {
			// 维修流程
			TabRepairFlow repairFlow = new TabRepairFlow();
			repairFlow.setId(entity.getFlowId());
			repairFlow.setFlowStep("3");
			repairFlow.setFlowStepState("1");
			repairFlow.setCarOwnerInfoId(entity.getCarOwnerInfoId());
			this.repairFlowMapper.updateByPrimaryKeySelective(repairFlow);
		}
		entity.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		return this.repairGjpgMapper.insert(entity);
	}

	@Override
	public int update(TabRepairGjpg entity) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.repairGjpgMapper.updateByPrimaryKey(entity);
	}
}
